﻿/*
 *  配置对象可以配置的方式列表,还有更多方式，就不一一列举，大同小异
 *  var dirPath = "MappingXMLs";
 *  var xmlFilePath = "MappingXMLs/User.hbm.xml";
 *  configuration.AddFile(xmlFilePath);
 *  configuration.AddXmlFile(xmlFilePath);
 *  configuration.AddXmlString(System.IO.File.ReadAllText(xmlFilePath));
 *  configuration.AddXmlReader(System.Xml.XmlReader.Create(System.IO.File.OpenRead(xmlFilePath)));
 *  configuration.AddClass(typeof(UserDomain.DataStore.Models.User));
 *  configuration.AddAssembly(typeof(UserDomain.DataStore.Models.User).Assembly);
 *  configuration.AddDirectory(new DirectoryInfo(dirPath));
 */

using NHibernate.Cfg;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;

namespace IceZero.DataStore
{
    public static class NHibernateHelperConfig
    {
        /// <summary>
        /// 默认配置规则约定,即根据程序目录下的 hibernate.cfg.xml 文件来配置
        /// </summary>
        /// <param name="configuration"></param>
        public static void ConfigByHibernateCfgXml(Configuration configuration)
        {
            //指定命名策略
            //configuration.SetNamingStrategy(ImprovedNamingStrategy.Instance);

            //使用默认配置规则约定,即根据程序目录下的 hibernate.cfg.xml 文件来配置
            //hibernate.cfg.xml
            /*
                 <?xml version="1.0" encoding="utf-8"?>
                <!-- nhibernate 配置节点 -->
                <hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
	                <!-- 会话工厂 -->
	                <session-factory name="IceZero">
		                <!-- 数据库驱动类配置 -->
		                <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
		                <!-- 数据库连接字符串配置 -->
		                <property name="connection.connection_string">
			                Database=icezero;Data Source=localhost;User Id=eform;Password=1qaz2WSX1qaz2WSX;
		                </property>
		                <!-- 数据库方言配置-->
		                <property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
		                <!-- 映射文件所在程序集配置 -->
		                <mapping assembly="IceZero.UserDomain.DataStore.Models" />
	                </session-factory>
                </hibernate-configuration>
             */
        }

        /// <summary>
        /// configuration.AddFile
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="filePaths"></param>
        public static void ConfigAddFile(Configuration configuration, List<string> filePaths)
        {
            foreach (var path in filePaths)
            {
                configuration.AddFile(path);
            }
        }

        /// <summary>
        /// configuration.AddClass
        /// </summary>
        /// <param name="configuration"></param>
        public static void ConfigAddClass(Configuration configuration, List<Type> types)
        {
            foreach (var type in types)
            {
                configuration.AddClass(type);

            }
        }

        /// <summary>
        /// configuration.AddAssembly
        /// </summary>
        /// <param name="configuration"></param>
        public static void ConfigAddAssembly(Configuration configuration, List<Assembly> assemblies)
        {
            foreach (var assembly in assemblies)
            {
                configuration.AddAssembly(assembly);

            }
        }

        /// <summary>
        /// configuration.AddDirectory
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="directories"></param>
        public static void ConfigAddDirectory(Configuration configuration, List<DirectoryInfo> directories)
        {
            foreach (var directory in directories)
            {
                configuration.AddDirectory(directory);

            }
        }

        /// <summary>
        /// configuration.AddInputStream
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="directories"></param>
        public static void ConfigAddInputStream(Configuration configuration, List<Stream> streams)
        {
            foreach (var stream in streams)
            {
                configuration.AddInputStream(stream);
            }
        }

        /// <summary>
        /// 配置命名策略
        /// </summary>
        /// <param name="configuration"></param>
        public static void ConfigSetNamingStrategy(Configuration configuration)
        {
            configuration.SetNamingStrategy(ImprovedNamingStrategy.Instance);
        }
    }
}
